<?php
/*
Page:           rpc.php
Created:        Aug 2006
Last Mod:       Mar 18 2007
This page handles the 'AJAX' type response if the user
has Javascript enabled.
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See credits.txt for full credit details.
--------------------------------------------------------- */

header("Cache-Control: no-cache");
header("Pragma: nocache");

require(dirname( dirname( dirname( dirname(__FILE__ )))).'/config/config.inc.php');
require(dirname( dirname(__FILE__ )).'/productratingeverning.php');
require('_config-rating.php');

$productrating_everning = new productratingeverning();

//getting the values
$vote_sent_everning = preg_replace('/[^0-9]/', '', $_REQUEST['j_everning']);
$vote_sent_everning = (int)$vote_sent_everning;
$id_sent_everning = preg_replace('/[^0-9a-zA-Z]/', '', $_REQUEST['q_everning']);
$id_sent_everning = (int)$id_sent_everning;
$ip_num_everning = preg_replace('/[^0-9\.]/', '', $_REQUEST['t_everning']);
$units_everning = preg_replace('/[^0-9]/', '', $_REQUEST['c_everning']);
$units_everning = (int)$units_everning;
$ip_everning = $_SERVER['REMOTE_ADDR'];

// kill the script because normal users will never see this.
if ($vote_sent_everning > $units_everning) 
	die('Sorry, vote appears to be invalid.'); 

// connecting to the database to get some information
$query_everning = mysql_query("SELECT `total_votes`, `total_value`, `used_ips` FROM `$rating_dbname_everning`.`$rating_tableName_everning` WHERE `id` = '$id_sent_everning'") or die("Error1: ".mysql_error());
$numbers_everning = mysql_fetch_assoc($query_everning);
$checkIP_everning = unserialize($numbers_everning['used_ips']);
$count_everning = $numbers_everning['total_votes']; // how many votes total
$current_rating_everning = $numbers_everning['total_value']; // total number of rating added together and stored
$sum_everning = $vote_sent_everning + $current_rating_everning; // add together the current vote value and the total vote value
$tense_everning = ($count_everning == 1) ? $productrating_everning->traduz('vote') : $productrating_everning->traduz('votes'); // plural form votes/vote

// checking to see if the first vote has been tallied 
// or increment the current number of votes
($sum_everning == 0 ? $added_everning = 0 : $added_everning = $count_everning + 1);

// if it is an array i.e. already has entries the push in another value
((is_array($checkIP_everning)) ? array_push($checkIP_everning, $ip_num_everning) : $checkIP_everning = array($ip_num_everning));
$insertip_everning = serialize($checkIP_everning);

// IP check when voting
$voted_everning = mysql_num_rows(mysql_query("SELECT `used_ips` FROM `$rating_dbname_everning`.`$rating_tableName_everning` WHERE `used_ips` LIKE '%".$ip_everning."%' AND `id` = '".$id_sent_everning."'"));
if (!$voted_everning) 
	// if the user hasn't yet voted, then vote normally...
	if (($vote_sent_everning >= 1 && $vote_sent_everning <= $units_everning) /*&& ($ip_everning == $ip_num_everning)*/) 
	{   
		// keep votes within range
		$update_everning = "UPDATE `$rating_dbname_everning`.`$rating_tableName_everning` SET `total_votes` = '".$added_everning."', `total_value` = '".$sum_everning."', `used_ips` = '".$insertip_everning."' WHERE `id` = '$id_sent_everning'";
		$result_everning = mysql_query($update_everning);		
	} 

// these are new queries to get the new values!
$newtotals_everning = mysql_query("SELECT `total_votes`, `total_value`, `used_ips` FROM `$rating_dbname_everning`.`$rating_tableName_everning` WHERE `id` = '$id_sent_everning'") or die("Error2: ".mysql_error());
$numbers_everning = mysql_fetch_assoc($newtotals_everning);
$count_everning = $numbers_everning['total_votes']; // how many votes total
$current_rating_everning = $numbers_everning['total_value']; // total number of rating added together and stored
$tense_everning = ($count_everning == 1) ? $productrating_everning->traduz('vote') : $productrating_everning->traduz('votes'); // plural form votes/vote

// $new_back_everning is what gets 'drawn' on your page after a successful 'AJAX/Javascript' vote
$new_back_everning = array();

$new_back_everning[] .= '<ul class="unit-rating-everning" style="width: '.$units_everning * $rating_unitwidth_everning.'px;">';
$new_back_everning[] .= '<li class="current-rating-everning" style="width: '.@number_format($current_rating_everning / $count_everning, 2) * $rating_unitwidth_everning.'px;">Current rating.</li>';
$new_back_everning[] .= '<li class="r1-unit">1</li>';
$new_back_everning[] .= '<li class="r2-unit">2</li>';
$new_back_everning[] .= '<li class="r3-unit">3</li>';
$new_back_everning[] .= '<li class="r4-unit">4</li>';
$new_back_everning[] .= '<li class="r5-unit">5</li>';
$new_back_everning[] .= '<li class="r6-unit">6</li>';
$new_back_everning[] .= '<li class="r7-unit">7</li>';
$new_back_everning[] .= '<li class="r8-unit">8</li>';
$new_back_everning[] .= '<li class="r9-unit">9</li>';
$new_back_everning[] .= '<li class="r10-unit">10</li>';
$new_back_everning[] .= '</ul>';
$new_back_everning[] .= '<p class="voted">'.$productrating_everning->traduz('Rating').': <strong>'.@number_format($sum_everning / $added_everning, 1).'</strong>/'.$units_everning.' ('.$count_everning.' '.$tense_everning.' '.$productrating_everning->traduz('cast').')';
$new_back_everning[] .= '<br /><span class="thanks">'.$productrating_everning->traduz('Thanks for voting!').'</span></p>';

$allnewback_everning = join("\n", $new_back_everning);

// name of the div id to be updated | the html that needs to be changed
$output_everning = "unit_long_everning$id_sent_everning|$allnewback_everning";
echo $output_everning;